/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.sourceforge.slcwsn.sensors;

import net.sourceforge.slcwsn.event.Event;
import net.sourceforge.slcwsn.event.EventVisitor;
import net.sourceforge.slcwsn.event.LostSensorMessagesEvent;
import net.sourceforge.slcwsn.event.PIRDetectionEvent;
import net.sourceforge.slcwsn.event.SensorAliveEvent;
import net.sourceforge.slcwsn.event.SensorChangedEvent;
import net.sourceforge.slcwsn.event.SensorDiscoveredEvent;
import net.sourceforge.slcwsn.event.SuperNodeDiscoveredEvent;
import net.sourceforge.slcwsn.network.Network;
import net.sourceforge.slcwsn.network.NetworkObserver;
import org.apache.log4j.Logger;

/**
 *
 * @author David Miguel Antunes <davidmiguel [ at ] antunes.net>
 */
public class SimpleNodeSensorVersion5 extends Sensor implements NetworkObserver, EventVisitor {

    private static final Logger logger = Logger.getLogger(SimpleNodeSensorVersion5.class);
    private long lastIAmAlive = 0;
    private long lastPIRDetection = 0;

    public SimpleNodeSensorVersion5(long id, Network network) {
        super(id, network, 256, 30);
        this.id = id;
        network.getObservers().add(this);
    }

    @Override
    public String toString() {
        return "SimpleNodeSensorVersion5 with id=" + id;
    }

    @Override
    public void accept(SensorVisitor visitor) {
        visitor.visit(this);
    }

    public void visit(SensorDiscoveredEvent event) {
    }

    public void visit(SensorAliveEvent event) {
        if (event.getSensor().equals(this)) {
            lastIAmAlive = event.getTimestamp();
            network.newEvent(new SensorChangedEvent(System.currentTimeMillis(), network, this));
        }
    }

    public void visit(PIRDetectionEvent event) {
        if (event.getSensor().equals(this)) {
            lastPIRDetection = event.getTimestamp();
            network.newEvent(new SensorChangedEvent(System.currentTimeMillis(), network, this));
        }
    }

    public void visit(SuperNodeDiscoveredEvent event) {
    }

    public void visit(LostSensorMessagesEvent event) {
    }

    public void visit(SensorChangedEvent event) {
    }

    public long getLastIAmAlive() {
        return lastIAmAlive;
    }

    public long getLastPIRDetection() {
        return lastPIRDetection;
    }

    public void newEvent(Event event) {
        event.accept(this);
    }
}
